<Window x:Class="ScrumSprintMonitor.ConfigEditor.Views.PresentationConfigurationEditorView.PresentationConfigurationEditorWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:WPFToolkit="clr-namespace:Microsoft.Windows.Controls;assembly=WPFToolkit"
    xmlns:local="clr-namespace:ScrumSprintMonitor.ConfigEditor"
    xmlns:converters="clr-namespace:ScrumSprintMonitor.ConfigEditor.Converters"
    xmlns:ConvertersLib="clr-namespace:ScrumSprintMonitor.UI.Wpf.Converters;assembly=ScrumSprintMonitor.UI.Wpf"
    Title="User Interface Configuration"
    Background="Gray"
    WindowStartupLocation="CenterOwner" ShowInTaskbar="False" Closed="Window_Closed" Width="600" MouseLeftButtonDown="Window_MouseLeftButtonDown">

    <Window.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="pack://application:,,,/ScrumSprintMonitor.UI.Wpf;component/Styles/FormResourceDictionary.xaml" />
            </ResourceDictionary.MergedDictionaries>

            <converters:PercentageValueConverter x:Key="PercentageValueConverter" />
            <ConvertersLib:ColorToBrushValueConverter x:Key="ColorToBrushValueConverter" />
            <ConvertersLib:RemainingWorkStatusTextValueConverter x:Key="RemainingWorkStatusTextValueConverter" />

            <local:XamlServiceLocator x:Key="serviceLocator" />
        </ResourceDictionary>
    </Window.Resources>

    <Window.Style>
        <StaticResource ResourceKey="DefaultChrome"/>
    </Window.Style>

    <DockPanel DataContext="{Binding Path=PresentationConfigurationEditorViewModel, Source={StaticResource serviceLocator}}">
        <Grid DockPanel.Dock="Top">
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="Auto" />
                <ColumnDefinition/>
            </Grid.ColumnDefinitions>
            <Grid.RowDefinitions>
                <RowDefinition Height="Auto" />
                <RowDefinition Height="Auto" />
                <RowDefinition Height="Auto" />
                <RowDefinition Height="Auto" />
                <RowDefinition Height="Auto" />
                <RowDefinition Height="Auto" />
                <RowDefinition Height="Auto" />
                <RowDefinition Height="Auto" />
                <RowDefinition Height="Auto" />
            </Grid.RowDefinitions>

            <ContentControl Grid.Row="0" Grid.ColumnSpan="2" ContentTemplate="{StaticResource Separator}" Content="Remaining Work Status" IsTabStop="False" Focusable="False" />

            <WPFToolkit:DataGrid x:Name="dataGrid" Grid.Row="1" Grid.ColumnSpan="2" AutoGenerateColumns="False" ItemsSource="{Binding RemainingWorkStatusRatioUpperLimits}" ItemStringFormat="" GridLinesVisibility="All" Foreground="Black" CanUserAddRows="False" CanUserDeleteRows="False" CanUserReorderColumns="False" CanUserResizeColumns="False" CanUserResizeRows="False" CanUserSortColumns="False" BorderThickness="1" AlternationCount="1" AlternatingRowBackground="WhiteSmoke" VerticalGridLinesBrush="Gainsboro" HorizontalGridLinesBrush="Cornsilk" HorizontalContentAlignment="Left" HorizontalAlignment="Stretch" HeadersVisibility="None" SelectionMode="Single" EnableRowVirtualization="False" AreRowDetailsFrozen="True">
                <WPFToolkit:DataGrid.Columns>
                    <WPFToolkit:DataGridTemplateColumn Header="Name" Width="Auto" IsReadOnly="True">
                        <WPFToolkit:DataGridTemplateColumn.CellTemplate>
                            <DataTemplate>
                                <TextBlock Text="{Binding Path=StatusPresentationConfig.Status, Mode=OneWay, Converter={StaticResource RemainingWorkStatusTextValueConverter}}" VerticalAlignment="Center" Margin="3" />
                            </DataTemplate>
                        </WPFToolkit:DataGridTemplateColumn.CellTemplate>
                    </WPFToolkit:DataGridTemplateColumn>
                    <WPFToolkit:DataGridTemplateColumn Header="Lower Limit" Width="Auto">
                        <WPFToolkit:DataGridTemplateColumn.CellTemplate>
                            <DataTemplate>
                                <TextBlock Text="{Binding Path=StatusPresentationConfig.LowerLimit, Converter={StaticResource PercentageValueConverter}}" HorizontalAlignment="Right" VerticalAlignment="Center" Margin="3" />
                            </DataTemplate>
                        </WPFToolkit:DataGridTemplateColumn.CellTemplate>
                        <WPFToolkit:DataGridTemplateColumn.CellEditingTemplate>
                            <DataTemplate>
                                <TextBox Text="{Binding Path=StatusPresentationConfig.LowerLimit, Converter={StaticResource PercentageValueConverter}}" HorizontalAlignment="Stretch" VerticalAlignment="Center" Margin="0" Padding="0" HorizontalContentAlignment="Stretch" TextAlignment="Right" IsReadOnly="{Binding Path=IsLowerLimitReadOnly}" Loaded="TextBox_Loaded" />
                            </DataTemplate>
                        </WPFToolkit:DataGridTemplateColumn.CellEditingTemplate>
                    </WPFToolkit:DataGridTemplateColumn>
                    <WPFToolkit:DataGridTemplateColumn Header="Upper Limit" Width="Auto">
                        <WPFToolkit:DataGridTemplateColumn.CellTemplate>
                            <DataTemplate>
                                <TextBlock Text="{Binding Path=StatusPresentationConfig.UpperLimit, Converter={StaticResource PercentageValueConverter}}" HorizontalAlignment="Right" VerticalAlignment="Center" Margin="3" />
                            </DataTemplate>
                        </WPFToolkit:DataGridTemplateColumn.CellTemplate>
                        <WPFToolkit:DataGridTemplateColumn.CellEditingTemplate>
                            <DataTemplate>
                                <TextBox Text="{Binding Path=StatusPresentationConfig.UpperLimit, Converter={StaticResource PercentageValueConverter}}" HorizontalAlignment="Stretch" VerticalAlignment="Center" Margin="0" Padding="0" HorizontalContentAlignment="Stretch" TextAlignment="Right" IsReadOnly="{Binding Path=IsUpperLimitReadOnly}" Loaded="TextBox_Loaded" />
                            </DataTemplate>
                        </WPFToolkit:DataGridTemplateColumn.CellEditingTemplate>
                    </WPFToolkit:DataGridTemplateColumn>
                    <WPFToolkit:DataGridTemplateColumn Header="Color" Width="80">
                        <WPFToolkit:DataGridTemplateColumn.CellTemplate>
                            <DataTemplate>
                                <Border BorderThickness="3" HorizontalAlignment="Stretch" Background="{Binding Path=StatusPresentationConfig.BackgroundColor, Converter={StaticResource ColorToBrushValueConverter}}" Margin="1, 0" CornerRadius="0.25,4,0.25,4">
                                    <TextBlock Text="Text" Foreground="White" VerticalAlignment="Center" Margin="3" />
                                </Border>
                            </DataTemplate>
                        </WPFToolkit:DataGridTemplateColumn.CellTemplate>
                        <WPFToolkit:DataGridTemplateColumn.CellEditingTemplate>
                            <DataTemplate>
                                <ComboBox x:Name="comboBox" ItemsSource="{Binding Path=PresentationConfigurationEditorViewModel.Colors, Source={StaticResource serviceLocator}}" SelectedItem="{Binding StatusPresentationConfig.BackgroundColor}" IsDropDownOpen="True">
                                    <ComboBox.ItemsPanel>
                                        <ItemsPanelTemplate>
                                            <VirtualizingStackPanel />
                                        </ItemsPanelTemplate>
                                    </ComboBox.ItemsPanel>

                                    <ComboBox.ItemTemplate>
                                        <DataTemplate>
                                            <Border Background="{Binding Path=., Converter={StaticResource ColorToBrushValueConverter}}" Margin="1, 0" CornerRadius="0.25,4,0.25,4">
                                                <TextBlock Text="Text" Foreground="White" HorizontalAlignment="Center" VerticalAlignment="Center" Margin="3" />
                                            </Border>
                                        </DataTemplate>
                                    </ComboBox.ItemTemplate>
                                </ComboBox>
                            </DataTemplate>
                        </WPFToolkit:DataGridTemplateColumn.CellEditingTemplate>
                    </WPFToolkit:DataGridTemplateColumn>
                </WPFToolkit:DataGrid.Columns>
            </WPFToolkit:DataGrid>
        </Grid>

        <StackPanel DockPanel.Dock="Bottom" HorizontalAlignment="Right" Orientation="Horizontal" VerticalAlignment="Bottom" Margin="0,12,0,0">
            <Button Command="{Binding OkCommand}" CommandParameter="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}}" Margin="6,0,0,0" Padding="16,4" IsDefault="True" Content="O_K"/>
            <Button Command="{Binding CancelCommand}" CommandParameter="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}}" x:Name="cancelButton" Margin="6,0,0,0" Padding="16,4" IsCancel="True" Content="Cancel"/>
        </StackPanel>
    </DockPanel>
</Window>
